package com.apchernykh.rozhlasapp.ui;

import com.apchernykh.ceskyrozhlas.basic.R;
import com.apchernykh.rozhlasapp.RozhlasPreferencesActivity;
import com.apchernykh.rozhlasapp.player.IPlaybackStateListener;
import com.apchernykh.rozhlasapp.player.NullPlaybackService;
import com.apchernykh.rozhlasapp.player.NullPlaybackStateListener;
import com.apchernykh.rozhlasapp.player.PlaybackControlView;
import com.apchernykh.rozhlasapp.services.ServiceMaster;
import com.apchernykh.rozhlasapp.ui.inetstate.IInetStateListener;
import com.apchernykh.rozhlasapp.ui.inetstate.NullInetStateListener;
import com.apchernykh.rozhlasapp.ui.podcasts.FeedsListActivity;
import com.apchernykh.rozhlasapp.utils.ConditionalLog;
import com.apchernykh.types.ApplicationModel;
import com.apchernykh.types.ApplicationModel.EPlaybackStatuses;


import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;


public class RozhlasAppBaseFragmentActivity extends android.support.v4.app.FragmentActivity implements IInetStateListener,
IPlaybackStateListener{

	//This variable should never be null... Unfortunately...
	static public ApplicationModel mApplicationModel = new ApplicationModel();
	
	public static void refreshPlaybackState(View iView)
	{
		View aPlayButton = iView.findViewById(R.id.button_play);
		if (aPlayButton != null) {
			BaseDetailsFragment.setOnPlayButtonListener(aPlayButton);
		}

		PlaybackControlView aPlaybackPane = (PlaybackControlView) iView.findViewById(R.id.playback_control_view);
		if (aPlaybackPane != null) {
			aPlaybackPane.syncToPlaybackState();
		}
	}
	
	public void refreshPlaybackState() {
		refreshPlaybackState(getWindow().getDecorView());
		onInetStatusChange(mApplicationModel.mInetInfo.isInetAvailable());
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mApplicationModel.mOnInetStateListener = this;
		mApplicationModel.mOnPlaybackStateListener = this;
		mServiceMaster.bind(this);
		refreshPlaybackState();
		RozhlasAppBaseFragmentActivity.mApplicationModel.mBackgroundPlaybackService.onReceivedFocus();
	}

	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		RozhlasAppBaseFragmentActivity.mApplicationModel.mBackgroundPlaybackService.onLostFocus();
		ApplicationModel.mNextRefresh = Long.MAX_VALUE;
		mServiceMaster.unbind(this);
		super.onPause();
		RozhlasAppBaseFragmentActivity.mApplicationModel.mOnPlaybackStateListener = new NullPlaybackStateListener();
		RozhlasAppBaseFragmentActivity.mApplicationModel.mOnInetStateListener = new NullInetStateListener();
	}

	ServiceMaster mServiceMaster = new ServiceMaster(new NullPlaybackService(), false);

	@Override
	public boolean onOptionsItemSelected(MenuItem iItem) {
		if (iItem.getItemId() == R.id.preferences_menu_item) {
			this.startActivity(new Intent(this,
					RozhlasPreferencesActivity.class));
			return true;

		} else if (iItem.getItemId() == R.id.podcast_menu_item) {
			this.startActivity(new Intent(this, FeedsListActivity.class));
			return true;
		}
		return super.onOptionsItemSelected(iItem);
	}

	void setCustomVisibility(int iViewId, boolean iVisible)
	{
		View aView = findViewById(iViewId);
		if(aView != null)
		{
			aView.setVisibility(iVisible?View.VISIBLE:View.GONE);
			aView.invalidate();
		}
	}
	
	@Override
	public void onInetStatusChange(final boolean iInetIsAvailable) {
		//In 2 seconds after inet switch on, all views will try to refresh (if they are exipred, of course)
		RozhlasAppBaseFragmentActivity.mApplicationModel.mBackgroundPlaybackService.reScheduleViewExpiration(System.currentTimeMillis() + 2000);		
		runOnUiThread(new Runnable() {			
			@Override
			public void run() {
				setCustomVisibility(R.id.no_connectivity_warning, !iInetIsAvailable);
			}
		});
	}
	
	@Override
	public void onPlaybackStatusChange(EPlaybackStatuses iStatus) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				refreshPlaybackState();
			}
		});
	}

	@Override
	protected void onCreate(Bundle iArg0) {
		ConditionalLog.mLogger.mLoggerFileName = "Android/"
				+ getString(R.string.app_name) + ".log";
		ConditionalLog.mLogger.open();
		super.onCreate(iArg0);
	}
}